/**
 * 方法调度器（ActionDispatcher），统一调用接口方法的方式。
 */
import getIn from './getIn';
import commonFetch from './commonFetch';
import _dispatch from './dispatch';
import Core from '../core/Core';
export function BaseActionCreator() {
    this.validate = () => {
        if (!this.core || !this.core.apiBase) {
            console.error('no apiBase');
            return false;
        }
        return true;
    };
    /**
     * 初始化，构造器
     * @param core
     */
    this.init = (core) => {
        this.core = core;
        console.log(core instanceof Core);
        this.validate();
    };
    this.getUrl = type => `${getIn(this, ['core', 'apiBase'])}${this.urls[type]}`;
    this.getParams = (params = {}) => {
        if (params.toJS) params = params.toJS();
        if (!params.domain) params.domain = getIn(this, ['core', 'domain']);
        return params;
    };
    /**
     * 执行接口方法
     * @param actionClass 接口信息类
     * @param params 参数集
     * @param callback 回调
     * @param extra 额外数据（不经过接口，提供额外的操作。reducer可用）
     */
    this.execute = (actionClass, params, callback, extra) => {
        return commonFetch(actionClass.type, this.getUrl(actionClass.url), this.getParams(params), extra, callback);
    };
}

/**
 *
 * @param ActionClass
 */
export function create(ActionClass) {
    // 创建一个没有实例方法的类
    const Super = function () {};
    Super.prototype = BaseActionCreator.prototype;
    // 将实例作为子类的原型
    ActionClass.prototype = new Super();
}

